package com.test.thread;

import java.util.concurrent.ArrayBlockingQueue;

public class CustomerDemo {
    private int queueSize=10;
    private ArrayBlockingQueue queue=new ArrayBlockingQueue(queueSize);

    public static void main(String[] args) {
        CustomerDemo demo=new CustomerDemo();
        Producer producer=demo.new Producer();
        Consumer consumer=demo.new Consumer();
        producer.start();
        consumer.start();
    }

     class Consumer extends  Thread{
        @Override
        public void run() {
            consume();
        }

        private void consume(){
            while(true){
                try {
                    queue.take();
                    System.out.println("从队列取走一个元素，队列剩余："+queue.size()+"个元素");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }
    }

    class Producer extends  Thread{
        @Override
        public void run() {
            produce();
        }
        private void produce(){
            while (true){
                try {
                    queue.put(1);
                    System.out.println("向队列中插入一个元素，队列剩余空间："+queue.size());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
